﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JYH.Data;
using Newtonsoft.Json;
using JYH.Model;
using JYH.DataAccess;
using JYH.Framework;
using JYH.Model.API;
using System.Runtime.CompilerServices;

namespace JYH.Business
{

    /// <summary>
    ///  SysUserInfo
    /// </summary> 
    public class SysUserInfoBLL : BusinessBase<SysUserInfo>
    {


        /// <summary>
        ///  SysUserInfo
        /// </summary> 
        public SysUserInfoBLL()
        {
            myDataBase = DB.D();
            this.SqlSelect = @"; Select SysUserInfo.* From SysUserInfo";
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="user">登录对象</param>
        /// <param name="Msg">反馈消息</param>
        /// <returns></returns>
        public SysUserInfo Login(SysUserInfo user,out string Msg)
        {
            Msg=string.Empty;
            SysUserInfo Info = new SysUserInfo();
            try
            {
                if (user == null)
                {
                    return null;
                }
                Sql sql=new Sql();
                sql.Append("Where SysUserInfo.UserNO='" +SqlFilterHelper.Filter( user.UserNO) + "'");
                Info= this.GetSingeModel(sql);
                if (Info == null)
                {

                    Msg = string.Format(SysMessageInfoBLL.GetMessageInfo("Login001"), user.UserNO);
                    return null;
                }
                else
                {
                    if (Info.RState != "1")
                    {
                        Msg = string.Format(SysMessageInfoBLL.GetMessageInfo("Login003"), user.UserNO);
                        return null;
                    }
                    else
                    {
                        if (!string.Equals(Info.Pwd, Security.DESEncrypt(user.Pwd)))
                        {
                            Msg = string.Format(SysMessageInfoBLL.GetMessageInfo("Login002"), user.UserNO);
                            return null;
                        }
                        else
                        {
                            return Info;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Msg = ex.Message;
                return null;
            }
            
        }

        public static SysUserInfo GetCookieUser()
        {
            var UserNO = CookieHelper.ReadCookie();
            Sql sql = new Sql();
            sql.Append(@"; Select SysUserInfo.* From SysUserInfo");
            sql.Append("Where SysUserInfo.UserNO='" + UserNO + "'");
            Database db = DB.D();
            return db.SingleOrDefault<SysUserInfo>(sql);
        }


   

        public bool SaveUserInfo(SysUserInfo user, out string Msg) 
        {
            Msg=string.Empty;
            try
            {
                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                SysUserInfo obj = new SysUserInfo();
                Sql sql = new Sql();
                if (string.IsNullOrEmpty(user.UserID))
                {
                    sql = new Sql();
                    sql.Append(@"Select SysUserInfo.* From SysUserInfo");
                    sql.Where(@"SysUserInfo.UserNO='" + user.UserNO + "'");
                    obj = myDataBase.SingleOrDefault<SysUserInfo>(sql);
                    if (obj != null && (obj.RState == "0" || obj.RState == "1"))
                    {
                        Msg = "User already exists";
                        return false;
                    }
                    else
                    {
                        if (obj == null)
                        {
                            if (string.Equals(user.UserNO.ToUpper(), "ADMIN"))
                            {
                                Msg = "User already exists";
                                return false;
                            }
                            obj = new SysUserInfo();
                            obj.UserID = Guid.NewGuid().ToString();
                            obj.UserNO = user.UserNO;
                            obj.Pwd = Security.DESEncrypt("123456");
                            obj.RState = "0";
                            obj.RegDate = BusinessTool.GetDate();
                            obj.UserName = user.UserName;
                            obj.CreateTime = BusinessTool.GetDateTime();
                            obj.CreateUser = GetCookieUser().UserNO;
                            myDataBase.Execute(BusinessTool.GetInsertSql(obj));
                        }
                        else
                        {
                            obj.RState = "0";
                            obj.UserName = user.UserName;
                            obj.UpdateTime = BusinessTool.GetDateTime();
                            obj.UpdateUser = GetCookieUser().UserNO;
                            myDataBase.Execute(BusinessTool.GetUpdateSql(obj));
                        }

                    }
                }
                else
                {
                    sql = new Sql();
                    sql.Append(@"Select SysUserInfo.* From SysUserInfo");
                    sql.Where(@"SysUserInfo.UserID='" + user.UserID + "'");
                    obj = myDataBase.SingleOrDefault<SysUserInfo>(sql);
                    if (obj == null)
                    {
                        Msg = "User not exists";
                        return false;
                    }
                    else
                    {
                        obj.UserNO = user.UserNO;
                        obj.UserName= user.UserName;
                        obj.UpdateTime = BusinessTool.GetDateTime();
                        obj.UpdateUser = GetCookieUser().UserNO;
                        myDataBase.Execute(BusinessTool.GetUpdateSql(obj));

                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                Msg=ex.Message; return false;
            }
        }


        /// <summary>
        /// 改变记录状态
        /// </summary>
        /// <param name="infos">用户集合</param>
        /// <param name="Msg">反馈信息</param>
        /// <returns></returns>
        public bool ChangeState(List<SysUserInfo> infos, string RState, out string Msg)
        {
            Msg=string.Empty;
            try
            {
                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                string UserID = "'',";
                string UserNO = "'',";
                foreach (var item in infos)
                {
                    UserID += "'" + item.UserID + "',";
                    UserNO += "'" + item.UserNO + "',";
                }
                StringBuilder sb = new StringBuilder();
                UserID = UserID.Substring(0, UserID.Length - 1);
                UserNO = UserNO.Substring(0, UserNO.Length - 1);
                Sql sql = new Sql();
                sql.Append(@"Select SysUserInfo.* From SysUserInfo");
                sql.Where(@"SysUserInfo.UserID in (" + UserID + ") or SysUserInfo.UserNO in (" + UserNO + ")");
                sql.OrderBy("SysUserInfo.UserNO");
                List<SysUserInfo> users = myDataBase.Fetch<SysUserInfo>(sql);
                if (users.Count < 1) 
                {
                    Msg = "No User exists";
                    return false;
                }
                foreach (var item in users) 
                {
                    if (!string.Equals(item.UserNO.ToUpper(),"ADMIN")) 
                    {
                        item.RState = RState;
                        sb.Append(BusinessTool.GetUpdateSql(item));
                    }
                }
                if (sb.Length > 0)
                {
                    myDataBase.Execute(sb.ToString());
                }
                return true;
            }
            catch (Exception ex)
            { 
                Msg=ex.Message; return false;
            }
        }


        public bool Reset(List<SysUserInfo> infos, out string Msg)
        {
            Msg = string.Empty;
            try
            {
                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                string UserID = "'',";
                string UserNO = "'',";
                foreach (var item in infos)
                {
                    UserID += "'" + item.UserID + "',";
                    UserNO += "'" + item.UserNO + "',";
                }
                StringBuilder sb = new StringBuilder();
                UserID = UserID.Substring(0, UserID.Length - 1);
                UserNO = UserNO.Substring(0, UserNO.Length - 1);
                Sql sql = new Sql();
                sql.Append(@"Select SysUserInfo.* From SysUserInfo");
                sql.Where(@"SysUserInfo.UserID in (" + UserID + ") or SysUserInfo.UserNO in (" + UserNO + ")");
                sql.OrderBy("SysUserInfo.UserNO");
                List<SysUserInfo> users = myDataBase.Fetch<SysUserInfo>(sql);
                if (users.Count < 1)
                {
                    Msg = "No User exists";
                    return false;
                }
                foreach (var item in users)
                {
                    if (!string.Equals(item.UserNO.ToUpper(), "ADMIN"))
                    {
                        item.Pwd = Security.DESEncrypt("123456");
                        sb.Append(BusinessTool.GetUpdateSql(item));
                    }
                }
                if (sb.Length > 0)
                {
                    myDataBase.Execute(sb.ToString());
                }
                return true;
            }
            catch (Exception ex)
            {
                Msg = ex.Message; return false;
            }
        }

    }

}
